#include <iostream>
#include <BinaryTree.hpp>

using namespace std;

class Solution
{
    int count = 0;
    int val = 0;

public:
    int kthSmallest(TreeNode *root, int k)
    {
        count = k;
        dfs(root);
        return val;
    }

    bool dfs(TreeNode *root)
    {
        if (root->left == nullptr && root->right == nullptr)
        {
            cout << root->val << ',';
            count--;
            val = root->val;
            return count == 0;
        }
        if (root->left != nullptr)
        {
            if (dfs(root->left))
                return true;
        }
        count--;
        cout << root->val << ',';
        if (count == 0)
        {
            val = root->val;
            return true;
        }
        if (root->right != nullptr)
        {
            if (dfs(root->right))
                return true;
        }
        return false;
    }
};

int main()
{
    cout << "Code230" << endl;
    Solution s;
    TreeNode *root = CreatTree("[5,3,6,2,4,null,null,1]", "null");
    cout << s.kthSmallest(root, 3);
    return 0;
}